package com.uphone.kingmall.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.TextView;

import com.china.zhuoyue.R;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/4/1
 *     desc   :
 * </pre>
 */
public class SubmitButton0 extends androidx.appcompat.widget.AppCompatButton {

    private int countViews = 0;

    public SubmitButton0(Context context) {
        this(context, null);
    }

    public SubmitButton0(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SubmitButton0(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundResource(R.drawable.shape_bg_ccc_fang);
    }

    public void setRelaViews(final TextView... textViews) {
        countViews = 0;
        for (TextView textView : textViews) {
            textView.addTextChangedListener(new TextWatcher() {
                boolean isEmpty = true;

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    if (TextUtils.isEmpty(s.toString())) {
                        isEmpty = true;
                    } else {
                        isEmpty = false;
                    }
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!TextUtils.isEmpty(s.toString())) {
                        if (isEmpty) {
                            countViews++;
                            if (countViews == textViews.length) {
                                //变红
                                setBackgroundResource(R.drawable.shape_bg_red_fang);
                            }
                        }
                    } else {
                        if (!isEmpty) {
                            countViews--;
                            //变灰
                            setBackgroundResource(R.drawable.shape_bg_ccc_fang);
                        }
                    }
                }
            });

        }
    }

}
